package com.product.system;

import com.sun.jmx.snmp.tasks.TaskServer;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class test2 {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Test
    public void  tes01() throws IOException {
//        InputStream bpmnStream= this.getClass().getClassLoader().getResourceAsStream("bpmn/qingjia.png");
//        InputStream xmlStream= this.getClass().getClassLoader().getResourceAsStream("bpmn/qingjia.bpmn20.xml");
//        System.out.println(bpmnStream);
//        System.out.println(xmlStream);

        InputStream inputStream_xml = new ClassPathResource("/bpmn/note.bpmn20.xml").getInputStream();
        InputStream inputStream_png = new ClassPathResource("/bpmn/note.png").getInputStream();

        //System.out.println(bpmnStream);
        System.out.println(inputStream_xml); //id:5805b668-65da-11ef-b0ea-005056c00001
        //部署流程
        Deployment deploy = repositoryService.createDeployment()
                .name("请假流程")
                //.addInputStream("qingjiabpmn", bpmnStream)
                .addInputStream("qingjiaxml",  inputStream_xml)
                .addInputStream("qingjiapng",inputStream_png)
                .deploy();
        System.out.println("---------------getKey="
                +deploy.getKey()+"-----name="+deploy.getName()+"---id="+deploy.getId());
    }

    @Test
    public void testSelect(){
        ProcessDefinition pr = repositoryService.createProcessDefinitionQuery().processDefinitionKey("note").singleResult();
        System.out.println(pr);
    }

    @Test
    public void statBpmn(){
        //chj请假
        //编写请假信息，//保存请假信息的主键id
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name1","chj");
       //hashMap.put("name2","xxx");
        //启动流程之后得到流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("note","100", hashMap);
        System.out.println("启动流程之后得到流程实例id:"+processInstance.getId()+"/////"+processInstance.getBusinessKey()+"业务ID");
        System.out.println("startProcessInstanceById:::::::::::::"+processInstance.getId());

        Task tesk = taskService.createTaskQuery().taskAssignee("chj").processInstanceId(processInstance.getId()).singleResult();
        HashMap<String, Object> paramx = new HashMap<>();
        paramx.put("name2","xxx");
        taskService.complete(tesk.getId(),paramx);//完成后送审

    }

/*    @Test
    public void test1(){
        //代办
        List<Task> tasks = taskService.createTaskQuery().taskAssignee("xxx").list();
    }

    @Test
    public void testx(){
        Task task;
        //审批
        taskService.complete("");
    }

 */


}
